package com.example.speeddating.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.entity.Message;
import com.example.speeddating.mapper.*;
import com.example.speeddating.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MatchMapper matchMapper;

    @Autowired
    private FollowMapper followMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public Page<Message> selectMessageList(Long id, int pageNum, int pageSize) {
        Page<Message> messagePage = new Page<>(pageNum, pageSize);
        Page<Message> page = messageMapper.selectMessageList(messagePage, id);
        for (Message message : page.getRecords()) {
            Integer integer = 0;
            if (message.getFromUser().getId().equals(id)){
                integer = messageMapper.selectCountUnread(message.getToUser().getId(), id);

            }else {
                integer = messageMapper.selectCountUnread(message.getFromUser().getId(), id);
            }
            message.setUnreadCount(integer);
        }
        return page;
    }

    @Override
    public Page<Message> selectMessageRecords(Long from, Long to, int pageNum, int pageSize) {
        Page<Message> messagePage = new Page<>(pageNum, pageSize);
        Page<Message> page = messageMapper.selectMessageRecords(messagePage, from, to);
        List<Message> records = page.getRecords();
        Collections.reverse(records);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addMessage(Message message) {
        messageMapper.insertMessage(message);
    }

    @Override
    public void updateUnreadMessage(Long from, Long id) {
        messageMapper.updateUnreadMessage(from,id);
    }

    @Override
    public HashMap<String, Object> getUnreadMessage(Long id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("match_unread",matchMapper.selectMatchUserCount(id));
        map.put("follow_unread",followMapper.selectUnReadCount(id));
//        未读评论与未读点赞
        map.put("reaction_unread",likeMapper.getUnreadCountLikeByArticleId(id)+likeMapper.getUnreadCountLIkeCommentInReplayComment(id)+commentMapper.getUnreadCountCommentByArticleId(id)+commentMapper.getUnreadCommentInReplayComment(id));
        return map;
    }

    /**
     * 删除会话
     * @param id
     * @param userId
     */
    @Override
    public void deleteConversation(Long id, Long userId) {

    }
}
